﻿Imports System.IO
Imports System.Reflection
Imports System.Runtime.CompilerServices

Public Module Util
    Public Enum eWWWFolder
        WWWROOT
        CAPTURES
    End Enum

    Public Function GetWWWFolder(ByVal FolderType As eWWWFolder) As String
        Select Case FolderType
            Case eWWWFolder.WWWROOT
                Return GetVirtualRoot()
            Case eWWWFolder.CAPTURES
                Return GetCaptureFolder()
        End Select
        Return ""
    End Function

    Private Function GetVirtualRoot() As String
        Dim VirtualRoot As String
        VirtualRoot = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules(0).FullyQualifiedName)
        VirtualRoot = VirtualRoot.TrimEnd("\") & "\WWW"
        Return VirtualRoot
    End Function

    Private Function GetCaptureFolder() As String
        Return GetVirtualRoot.TrimEnd("\") & "\Captures"
    End Function

    <Extension()> _
    Public Function URLDecode(ByVal Input As String) As String
        Input = Input.Replace("%5C", "\")
        Input = Input.Replace("%2F", "/")
        Input = Input.Replace("%3F", "?")
        Input = Input.Replace("%20", " ")
        Input = Input.Replace("%7B", "{")
        Input = Input.Replace("%7D", "}")
        Input = Input.Replace("%60", "`")
        Input = Input.Replace("%40", "@")
        Input = Input.Replace("%3A", ":")
        Input = Input.Replace("%21", "!")
        Input = Input.Replace("%2A", "*")
        Input = Input.Replace("%27", "'")
        Input = Input.Replace("%28", "(")
        Input = Input.Replace("%29", ")")
        Input = Input.Replace("%3B", ";")
        Input = Input.Replace("%26", "&")
        Input = Input.Replace("%3D", "=")
        Input = Input.Replace("%2B", "+")
        Input = Input.Replace("%24", "$")
        Input = Input.Replace("%2C", ",")
        Input = Input.Replace("%23", "#")
        Input = Input.Replace("%5B", "[")
        Input = Input.Replace("%5D", "]")
        Input = Input.Replace("%3C", "<")
        Input = Input.Replace("%3E", ">")
        Input = Input.Replace("%7E", "~")
        Input = Input.Replace("%2E", ".")
        Input = Input.Replace("%3A", ":")
        Input = Input.Replace("%2D", "-")
        Input = Input.Replace("%5F", "_")
        Input = Input.Replace("%5E", "^")
        Input = Input.Replace("%22", """")
        Input = Input.Replace("%0A", vbNewLine)


        Return Input.Replace("%25", "%") 'Make sure the latest one
    End Function

    Public Function TimeSpanFormat(ByVal StartTime As Date, ByVal EndTime As Date) As String
        Dim TS = EndTime - StartTime
        Return JsonEncoder.TimeSpanFormat(TS)
    End Function

    Public Function ToLocalDate(ByVal S As String) As Date
        'Converts from m/d/Y to local date !
        Try
            Dim LSep = S.Split("/"(0)).ToList
            Dim m As Integer = CInt(LSep(0))
            Dim d As Integer = CInt(LSep(1))
            Dim Y As Integer = CInt(LSep(2))
            Dim Dt As Date = New Date(Y, m, d)
            Return Dt
        Catch ex As Exception
            Return New Date(1900, 1, 1)
        End Try
    End Function
End Module
